/* eslint-disable eqeqeq */
export default {
  methods: {
    // 处理商品的spec_list规格列表
    setSpec_list(skuList, type) {
      const data = []
      switch (type) {
        case 'none': // 无规格
          data.push({
            list: [{
              name: '默认'
            }],
            name: '默认'
          })
          break
        default: // 多规格
          skuList.forEach(item => {
            data.push({
              list: item.v,
              name: item.k
            })
          })
          break
      }
      return data
    },
    // 处理商品的Sku_list sku列表
    setSku_list(goodsItem, type) {
      let data = []
      switch (type) {
        case 'none': // 无规格
          data.push({
            _id: goodsItem.id,
            goods_id: goodsItem.id,
            goods_name: goodsItem.name,
            image: goodsItem.iconId[0].url,
            sku_name_arr: ['默认'],
            price: goodsItem.price,
            stock: goodsItem.stock_num,
            cart_num: goodsItem.cart_num || null, // 购物车选择的数量
            cartId: goodsItem.cartId || null // 购物车list id
          })
          break
        default: // 多规格
          goodsItem.list.forEach(item => { // 先循环数组
            item['indexList'] = []
            for (const prop in item) { // 循坏对象(匹配规格列表)
              goodsItem.skuList.forEach((value, idx) => {
                if (prop == value.k_s) { // 匹配到规格列表父类
                  const index = value.v.findIndex(i => i.id == item[prop])
                  item['indexList'].push({
                    parentIndex: idx,
                    sonIndex: index
                  })
                }
              })
            }
            data.push({
              _id: item.id,
              goods_id: goodsItem.id,
              goods_name: goodsItem.name,
              image: '',
              sku_name_arr: [],
              price: item.price,
              stock: item.stock_num,
              indexList: item.indexList,
              cart_num: item.cart_num || null, // 购物车选择的数量
              cartId: item.cartId || null // 购物车list id
            })
          })
          // 匹配了基础再匹配sku头像和sku名称
          data.forEach(item => {
            item.indexList.forEach(value => {
              item.sku_name_arr.push(goodsItem.skuList[value.parentIndex].v[value
                .sonIndex].name)
              item.image += goodsItem.skuList[value.parentIndex].v[value.sonIndex]
                .previewImgUrl.length > 0 ? goodsItem.skuList[value.parentIndex].v[
                  value.sonIndex].previewImgUrl[0].url : ''
            })
            item.image = item.image ? item.image : goodsItem.iconId[0].url
          })
          break
      }
      return data
    }
  }
}
